package smzdm_pages;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public abstract class BasePage {



	protected WebDriver driver = null;

	public BasePage(WebDriver driver) {
		if (null == driver) {
			throw new IllegalArgumentException("WebDriver cannot be null.");
		}
		this.driver = driver;
	}

	public WebDriver getWebDriver() {
		return driver;
	}

	public String getCurrentUrl() {
		return driver.getCurrentUrl();
	}

	public WebElement findElement(By by) {
		return driver.findElement(by);
	}

	public List<WebElement> findElements(By by) {
		return driver.findElements(by);
	}

	public void sleep(int seconds) {
		try {
			TimeUnit.SECONDS.sleep(seconds);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public boolean wait(final By by) {
		new WebDriverWait(driver, 10).until(new ExpectedCondition<Boolean>() {
			public Boolean apply(WebDriver arg0) {
				return arg0.findElement(by).isDisplayed();
			}
		});
		return true;
	}

	public Alert getAlert() {
		sleep(2);
		Alert alert = driver.switchTo().alert();
		return alert;
	}

	public void KeyDownAndEnter() {
		sleep(1);
		Actions actions = new Actions(driver);
		actions.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build()
				.perform();
	}

	public void KeyEnter() {
		sleep(1);
		Actions actions = new Actions(driver);
		actions.sendKeys(Keys.ENTER).build().perform();
	}

	public void switchToNewPage() {
		sleep(1);
		String handleNew = "";
		for (String handle : driver.getWindowHandles()) {
			handleNew = handle;
		}
		driver.close();
		sleep(1);
		driver.switchTo().window(handleNew);
	}

	public void openAndWait(String url) {
//		WebDriverBackedSelenium selenium = new WebDriverBackedSelenium(driver, url);
		driver.get(url);
//		selenium.waitForPageToLoad(timeout);
//		selenium.waitForPageToLoad("50000");
		WebDriverWait w = new WebDriverWait(driver ,5);
	
		sleep(5);
	}


}